/**
    @file camera_factory.cpp
    @brief A factory object for managing cameras
*/
#include "camera_factory.hpp"

#include <OgreRenderWindow.h>

template<> pm3::CAMERA_FACTORY* Ogre::Singleton<pm3::CAMERA_FACTORY>::ms_Singleton = 0;
pm3::CAMERA_FACTORY pm3::g_camera_factory;

pm3::CAMERA_FACTORY::CAMERA_FACTORY() {
    
}

Ogre::PCZCamera *pm3::CAMERA_FACTORY::create_camera( const std::string &s_name ) {
    Ogre::PCZSceneManager *scene_mgr = pm3::INTERFACE::getSingleton().get_scenemgr();
    pm3::INTERFACE *intf = pm3::INTERFACE::getSingletonPtr();

    // Does such a camera already exist?
    if (scene_mgr->hasCamera( s_name )) {
        // Destroy it
        destroy_camera( s_name );
    }

    Ogre::PCZCamera *cam = (Ogre::PCZCamera *) scene_mgr->createCamera( s_name );
    cam->setNearClipDistance( 0.1f );
    cam->setFarClipDistance( 1000.0f );

    Ogre::Viewport *vport = intf->get_render_window()->addViewport( cam );
    cam->setAspectRatio( Ogre::Real( 4.0f / 3.0f ) );

    return cam;
}

void pm3::CAMERA_FACTORY::destroy_camera( const std::string &s_name ) {
    Ogre::PCZSceneManager *scene_mgr = pm3::INTERFACE::getSingleton().get_scenemgr();
    pm3::INTERFACE *intf = pm3::INTERFACE::getSingletonPtr();

    intf->get_render_window()->removeViewport( 0 );

    scene_mgr->destroyCamera( s_name );
}

void pm3::CAMERA_FACTORY::destroy_camera( Ogre::PCZCamera *p_cam ) {
    Ogre::PCZSceneManager *scene_mgr = pm3::INTERFACE::getSingleton().get_scenemgr();
    pm3::INTERFACE *intf = pm3::INTERFACE::getSingletonPtr();

    intf->get_render_window()->removeViewport( 0 );

    scene_mgr->destroyCamera( p_cam );
}

